<?php

namespace App\Http\Controllers\Manage;


use App\Models\SiteInfo;
use App\Models\User;
use App\Models\UserAuth;

class CourseController extends BaseController
{

    // 课程关了
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Course';
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 活动信息
        $eids  = array_column($lists, 'auth_id');
        $auths = (new UserAuth())->isCache(true)->getListInIds($eids);


        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            //$operate .= $listHtml->operate_edit(['id' => $_id], '编辑课程');
            $operate .= $listHtml->operate_delete(['id' => $_id], '你确定要删除该课程吗？');
            // 指定链接
            $operate_url = $listHtml->operate_list(['course_id' => $_id, 'is_pop' => 1,], '课程周期', '/course-cycle/lists');
            $operate_url .= $listHtml->operate_list(['course_id' => $_id, 'is_pop' => 1,], '课程时间', '/course-times/lists');
            $operate_url .= $listHtml->operate_list(['course_id' => $_id, 'is_pop' => 1,], '课程学员', '/course-student/lists');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['id']);
            $tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($users[$item['auth_id']]['auth_name'] ?? '-');
            $tr .= $listHtml->list_td($item['name']);
            $tr .= $listHtml->list_td($item['times']);
            $tr .= $listHtml->list_td($item['start_time'] ? date('Y-m-d H:i', $item['start_time']) : '-');
            $tr .= $listHtml->list_td($item['end_time'] ? date('Y-m-d H:i', $item['end_time']) : '-');
            $tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate_url);
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/manage/course/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加课程</a> ';
        //$this->assign('top_button', $top_button);
        $this->assign('top_button', '');
    }

    // 列表字段
    protected function list_fields()
    {
        return ['id' => 'ID', 'uid' => '用户名', 'auth_id' => '认证名', 'name' => '课程', 'times' => '课时', 'start_time' => '开始时间', 'end_time' => '结束时间', 'status' => '状态', 'ctime' => '创建时间', 'url' => '链接', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();


        //$auth_id = $form->formInput('auth_id', $request['auth_id'] ?? '', '认证名', '200px');
        //$uid     = $form->formInput('uid', $request['uid'] ?? '', '用户名');
        $uid     = $form->ajaxSingleFromList('uid', '用户ID', 'User', 'id', 'username');
        $auth_id = $form->ajaxSingleFromList('auth_id', '认证ID', 'Auth', 'id', 'auth_name');
        $name    = $form->formInput('name', $request['name'] ?? '', '课程名', '200px');

        $search = ['name' => $name, 'auth_id' => $auth_id, 'uid' => $uid,];  // 'username' => $username,
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        // 获取用户信息
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $aInfo = (new UserAuth())->isCache(true)->getOneById($info['auth_id'] ?? 0);

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        $status      = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $uid         = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $auth_id     = $form->ajaxSingleFromList('auth_id', $aInfo, 'User', 'id', 'username');
        $name        = $form->formInput('name', $info['name'] ?? '', '课程名');
        $times       = $form->formNumber('times', $info['times'] ?? '', '课时数');
        $start_time  = $form->formDateTime('start_time', $info['start_time'] ?? "", '开始时间', 'datetime');
        $end_time    = $form->formDateTime('end_time', $info['end_time'] ?? "", '结束时间', 'datetime');
        $description = $form->formTextarea('description', $info['description'] ?? '', '描述');
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户名', $uid);
        $html .= $form->makeItem('认证名', $auth_id);
        $html .= $form->makeItem('课程名', $name);
        $html .= $form->makeItem('课时数', $times);
        $html .= $form->makeItem('开始时间', $start_time);
        $html .= $form->makeItem('结束时间', $end_time);
        $html .= $form->makeItem('状态', $status);
        $html .= $form->makeItem('描述', $description);
        $html .= $_id;

        return $html;
    }

}


